package some.member.util;

import java.io.File;

public class FileUtil {
	//업로드 경로
	public static final String UPLOAD_PATH = "C:/javaWork/workspace_spring/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/SomeTalk/upload";
	
	/**
	 * �뚯씪 �대쫫 蹂�꼍, 蹂�꼍���대쫫��硫붿냼�쒕궡�먯꽌 吏�젙
	 * */
	public static String rename(String filename) throws Exception{
		
		if(filename ==null) return null;
		String newName = 
		Long.toString(System.currentTimeMillis())+(int)(Math.random()*50);
		
		return rename(filename, newName);
	}
	/**
	 * �뚯씪 �대쫫 蹂�꼍, 蹂�꼍���대쫫��硫붿냼���몄텧���뚮쭏��吏�젙 媛�뒫
	 * */
	public static String rename(String filename, String newName) 
	                                            throws Exception{
		
		if(filename == null) return null;

		//�뚯씪紐낆쓣 �먰븯���뺤떇�쇰줈 蹂�꼍�섍린
		int idx = filename.lastIndexOf(".");
		String extention = "";
		String newFileName = "";

		if(idx != -1) {
			extention = filename.substring(idx);
		}
		// �뺤옣�먮� �쒖쇅�����뚯씪紐낆쓣 �낅젰�댁빞 �섎뒗���뺤옣�먮� �ы븿�댁꽌
		// �낅젰�덈떎硫��뺤옣�먮� �쒖쇅�섍퀬 �뚯씪紐낆쑝濡��뗮똿
		int newIdx = newName.lastIndexOf(".");
		if(newIdx !=-1){
			newName = newName.substring(0,newIdx);
		}

		newFileName = newName + extention.toLowerCase();
		
		return newFileName;
	}
	/**
	 * 파일 삭제
	 * */
	public static void removeFile(String filename){
		File file = new File(UPLOAD_PATH,filename);
		if(file.exists()) file.delete();
		
	}
}
